package com.baggio.locationex;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.baggio.locationex.location.BgLocation;
import com.baggio.locationex.location.baidu.BaiduLocation;
import com.baggio.locationex.platform.BgLocationData;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;

import static com.baggio.locationex.location.BgLocationType.LOCATION_TYPE_BAIDU;
import static com.baggio.locationex.location.BgLocationType.LOCATION_TYPE_GAODE;

public class MainActivity extends AppCompatActivity implements MainActivityCallback {

    private BgLocation bgLocation = null;

    private Button btnStart = null;
    private Boolean isStarted = false;

    private TextView latitudeText = null;
    private TextView longitudeText = null;

    private RadioButton radioButtonBaidu = null;
    private RadioButton radioButtonGaode = null;

    private MapView mapViewBaidu = null;
    private BaiduMap baiduMap = null;

    private MainActivityCallback callback = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        InitializeResource();
        InitializeEvents();
    }

    private void InitializeResource() {
        latitudeText = findViewById(R.id.latitudeText);
        longitudeText = findViewById(R.id.longitudeText);

        btnStart = findViewById(R.id.buttonStart);

        radioButtonBaidu = findViewById(R.id.radioButtonBaidu);
        radioButtonGaode = findViewById(R.id.radioButtonGaode);

//        SDKInitializer.initialize(getApplicationContext());
        SDKInitializer.setCoordType(CoordType.BD09LL);

        mapViewBaidu = findViewById(R.id.baiduMapView);
        baiduMap = mapViewBaidu.getMap();
        // 开启定位图层
        baiduMap.setMyLocationEnabled(true);

        radioButtonBaidu.setChecked(true);
    }

    private void InitializeEvents() {
        // 定义按钮事件
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 这里是点击开始按钮后的工作
                if (!isStarted) {

                    // 根据选中情况初始化对应的定位SDK
                    if (radioButtonBaidu.isChecked()) {
                        bgLocation = new BgLocation(LOCATION_TYPE_BAIDU, callback);
                    } else {
                        bgLocation = new BgLocation(LOCATION_TYPE_GAODE, callback);
                    }

                    bgLocation.Initialize(getApplicationContext());

                    bgLocation.Start();

                    btnStart.setText(R.string.main_button_stop);
                    isStarted = true;
                } else {
                    bgLocation.Stop();

                    btnStart.setText(R.string.main_button_start);
                    isStarted = false;
                }
            }
        });

        radioButtonBaidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 检查自己是否被选中
                if (radioButtonBaidu.isChecked()) {
                    radioButtonGaode.setChecked(false);
                    Toast.makeText(getApplicationContext(), "radioButtonBaidu 被选中！", Toast.LENGTH_SHORT).show();
                }
            }
        });

        radioButtonGaode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (radioButtonGaode.isChecked()) {
                    radioButtonBaidu.setChecked(false);
                    Toast.makeText(getApplicationContext(), "radioButtonGaode 被选中！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void UpdateLocationData(BgLocationData locationData) {

        latitudeText.setText(String.format("%f", locationData.latitude));
        longitudeText.setText(String.format("%f", locationData.longitude));

        // 地图画定位信息
        MyLocationData myLocationData = new MyLocationData.Builder()
                .accuracy(locationData.radius).direction(locationData.direction)
                .latitude(locationData.latitude).longitude(locationData.latitude).build();

        baiduMap.setMyLocationData(myLocationData);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapViewBaidu.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapViewBaidu.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapViewBaidu.onDestroy();
    }
}
